<?php

namespace App\Admin\Controllers;

use App\Services\NodeManagementService;
use Slowlyo\OwlAdmin\Controllers\AdminController;
use App\Support\Components;

/**
 * 节点管理
 *
 * @property NodeManagementService $service
 */
class NodeManagementController extends AdminController
{
	protected string $serviceName = NodeManagementService::class;


	public function index()
    {
        if ($this->actionOfGetData()) {
            return $this->response()->success($this->service->list());
        }
        return $this->response()->success($this->page());
    }

	public function page()
    {
        return amis()->Page()->body(
            amis()->Flex()->className('w-full')->items([
                amis()->Card()->className('w-1/6 mr-5 mb-0')->body([
                    // 用 Nav 实现边栏
                    amis()
                        ->Nav()
						->defaultOpenLevel(5)
                        ->stacked()
						->indentSize(20)	
						->mode('inline')
                        ->links($this->service->tree())
                        ->name('tree'),
                    
                ]),
                $this->list(),
            ])
        );
    }




	public function list()
	{
		$typeId = request()->input('type_id');
		$idcId = request()->input('idc_id');

		$crud = $this->baseCRUD()
			->filterTogglable()
			->filterDefaultVisible(false) // 隐藏筛选栏
			->headerToolbar([
				$this->createButton('dialog'),
				...$this->baseHeaderToolBar()
			])
			->filter($this->baseFilter()->body([
				amis()->HiddenControl('type_id')->value($typeId),
				amis()->HiddenControl('idc_id')->value($idcId)
			]))
			->columns([
				amis()->TableColumn('id', 'ID')->sortable(),
				amis()->TableColumn('name', '节点名称'),
				// amis()->TableColumn('customer.customer_name', '关联客户'),
				amis()->TableColumn('reference_95_value', '95参考值'),
				amis()->TableColumn('yiyong', '已用点位'),
				amis()->TableColumn('yiyong_minutes', '已用分钟'),
				amis()->TableColumn('shengyu', '剩余点位'),
				amis()->TableColumn('shengyu_minutes', '剩余分钟'),

				
				amis()->TableColumn('node_remark', '节点备注'),
				amis()->TableColumn(' ', '操作')->type('operation')->buttons([
					$this->portchart('dialog'),
                ]),
				amis()->TableColumn(' ', '操作')->type('operation')->buttons([
					$this->rowEditButton('dialog'),
					$this->rowDeleteButton(),
				]),
			
			]);

		return $this->baseList($crud);
	}

	public function form($isEdit = false)
	{
		return $this->baseForm()->body([
			
			amis()->SelectControl('type_id', '节点分类')
				->options([
					['label' => '客户侧流量统计', 'value' => 1],
					['label' => '供应商流量统计', 'value' => 2],
					['label' => '运营商流量统计', 'value' => 3],
				]),
			
			amis()->GroupControl()->body([
				amis()->SelectControl('fcustomer_id', '关联客户')
					->source('/fcustomer?_action=getData')
					->searchable(true)
					->labelField('short_name')
					->valueField('id'),
				amis()->SelectControl('related_customers', '业务名称')
					->searchable(true)
					->source('/customer_bus?_action=getData&fcustomer_id=${fcustomer_id}')
					->labelField('name')
					->valueField('id')
					->autoFill([
						'idc_id' => '${idcnode.id}',
						'idcnode_name' => '${idcnode.idc_name}',
						'short_name' => '${fcustomer.short_name}',
						'name' => '${fcustomer.short_name}-${idcnode.idc_name}-${name}'
					]),	
			])->disabledOn('data.type_id >1')->hiddenOn('data.type_id >1'),

			amis()->GroupControl()->body([
				amis()->SelectControl('up_customer_id', '关联上游')
					->source('/up_customer?_action=getData')
					->searchable(true)
					->labelField('short_name')
					->valueField('id'),
				amis()->SelectControl('related_customers', '业务名称')
					->searchable(true)
					->source('/upcustomer_bus?_action=getData&up_customer_id=${up_customer_id}')
					->labelField('name')
					->valueField('id')
					->autoFill([
						'idcnode_name' => '${idcnode.idc_name}',
						'idc_id' => '${idcnode.id}',
						'short_name' => '${upcustomer.short_name}',
						'name' => '${upcustomer.short_name}-${idcnode.idc_name}-${name}'
					]),	
			])->disabledOn('data.type_id == 1')->hiddenOn('data.type_id == 1'),		


			amis()->GroupControl()->body([
				amis()->TextControl('short_name', '客户简称')->static()->hiddenOn('data.type_id >1'),
				amis()->TextControl('short_name', '上游简称')->static()->hiddenOn('data.type_id == 1'),
				amis()->TextControl('idcnode_name', '所在机房')->static(),
				amis()->TextControl('idc_id', '所在机房ID')->type('hidden'),
			]),
			amis()->TextControl('name', '节点名称'),
			amis()->GroupControl()->body([
				amis()->SelectControl('related_switches', '关联交换机')
					->source('/switch?_action=getData')
					->searchable(true)
					->labelField('name')
					->valueField('id'),
				amis()->SelectControl('port_list', '端口列表')
					->multiple(true)
					->searchable(true)
					->source('/switch_port?_action=getData&switchId=${related_switches}')
					->labelField('name')
					->valueField('id'),
			]),
			amis()->TextControl('reference_95_value', '95参考值'),

			amis()->TextControl('sort_order', '排序'),
			amis()->TextareaControl('node_remark', '节点备注'),
		]);
	}
	/**
	 * 查看客户端口流量
	 * @return mixed
	 */
	public function portchart()
	{
		$action = amis()->DialogAction()
			->label('查看端口流量')
			->icon('fa-solid fa-chart-line')
			->level('info')
			->dialog(
				amis()->Dialog()
					->title('端口流量统计（包含${port_list|split|count}个端口的流量统计）')
					->size('full')
					->body([
						amis()->Grid()->columns([
							amis()->GridColumn()->md(12)->body([
								// 时间范围选择
								amis()->Form()
									->id('flowchartForm')
									// 去掉边框
									->wrapWithPanel(false)
									->title('')
									->submitText('')
									->data([
										'timeRange' => '24th',
										'dataType' => '1000'
									])
									->body([
										amis()->Grid()->className('mb-4')->columns([
											amis()->GridColumn()->md(10)->body(
												amis()->ButtonGroupControl()
													->name('timeRange')
													->label('')
													->value('24th')
													->onEvent([
														'change' => [
															'actions' => [
																[
																	'actionType' => 'reload',
																	'componentId' => 'flowChart',
																	'args' => ['timeRange' => '${timeRange}']
																]
															]
														]
													])
													->options([
														['label' => '最近24小时', 'value' => '24th'],
														['label' => '今天', 'value' => 'today'],
														['label' => '最近七天', 'value' => 'last7days'],
														['label' => '本周', 'value' => 'week'],
														['label' => '上周', 'value' => 'lastweek'],
														['label' => '本月（截止当前）', 'value' => 'month'],
														['label' => '本月（完整月份）', 'value' => 'month_full'],
													])
											),
											amis()->GridColumn()->md(2)->body(
												amis()->ButtonGroupControl()
													->labelAlign('right')
													->name('dataType')
													->label('')
													->btnActiveLevel('danger')
													->value('1000')
													->onEvent([
														'change' => [
															'actions' => [
																[
																	'actionType' => 'reload',
																	'componentId' => 'flowChart',
																	'args' => ['dataType' => '${dataType}']
																]
															]
														]
													])
													->options([
														['label' => '1000', 'value' => '1000'],
														['label' => '1024', 'value' => '1024'],
													])
											)
										])
									]),
								// 流量图表
								amis()->Card()->body([
									Components::make()->trafficChart('node','lg','${id}')->id('flowChart')
								]),
							

								// 输出一段文字
								amis()->Tpl()->tpl('包含对象节点图'),
								
								amis()->Grid()
									->columns([
										amis()->Each()
											->type('each')
											->source('${port_list|split}')
											->className('cxd-Grid cxd-Grid--gap-4')
											->items(
												amis()->Card()->className('cxd-Grid-col--md6')->body([
													Components::make()->trafficChart('single','md', '${item}')
														->id('flowChart_${item}')
												])
											)
									]),
							]),	
						])
					])
			);
		return $action;
	}
}
